package com.tbgov.util;

import com.alibaba.fastjson2.JSONObject;
import com.tbgov.entity.pojo.Token;
import com.tbgov.service.RedisTokenService;
import io.jsonwebtoken.lang.Assert;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 获取token用户登录信息工具类
 *
 * @author 1st
 */
public class TokenUtil {
    public static Token getLoginUser() {
        ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
        HttpServletRequest request = null;
        if (servletRequestAttributes != null) {
            request = servletRequestAttributes.getRequest();
        }
        Assert.isTrue(request != null, "当前用户未登录，无法获取用户信息");
        RedisTokenService jedis = SpringContextUtil.getBean(RedisTokenService.class);
        String token = jedis.getLoginUser(request.getHeader("token"));
        Assert.isTrue(token != null, "登录信息过期，请重新登录");
        return JSONObject.parseObject(token, Token.class);
    }
}
